/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package org.eclipse.iota.model;

import java.util.Collection;

import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.EObjectImpl;
import org.eclipse.emf.ecore.util.EObjectContainmentWithInverseEList;
import org.eclipse.emf.ecore.util.InternalEList;
import org.eclipse.iota.IotaPackage;

/**
 * <!-- begin-user-doc -->
 * A representation of the model object '<em><b>Alternation</b></em>'.
 * <!-- end-user-doc -->
 *
 * <p>
 * The following features are supported:
 * <ul>
 *   <li>{@link org.eclipse.iota.model.Alternation#getClauses <em>Clauses</em>}</li>
 * </ul>
 * </p>
 *
 * @see org.eclipse.iota.IotaPackage#getAlternation()
 * @model kind="class"
 * @generated
 */
public class Alternation extends EObjectImpl implements Element, Predicate {

	/**
	 * @generated NOT
	 */
	public Alternation(Clause... clauses) {
		for (Clause clause : clauses) {
			getClauses().add(clause);
		}
	}

	/**
	 * @model kind="operation"
	 * @generated NOT
	 */
	public boolean isTrue() {
		// at least one clause must be true
		for (Clause clause : getClauses()) {
			if (clause.isTrue()) return true;
		}
		return false;
	}

	/**
	 * @model kind="operation"
	 * @generated NOT
	 */
	public boolean isFalse() {
		// all clauses must be false
		for (Clause clause : getClauses()) {
			if (!clause.isFalse()) return false;
		}
		return true;
	}

	/**
	 * @model kind="operation"
	 * @generated NOT
	 */
	public Definition getDefinition() {
		return (eContainer() instanceof Definition) ? (Definition) eContainer() : null;
	}
	
	/**
	 * @generated NOT
	 */
	@Override
	public String toString() {
		String result = "";
		int size = getClauses().size();
		for (int i=0; i<size; i++) {
			result = result + "\t" + clauses.get(i);
			if (i<size-1) result = result + " |\n";
		}
		return result + "\n";
	}
	
	/* ---------------------------------------------------------------- *
	 * GENERATED CODE.                                                  *
	 * Do not edit below this line. If you need to edit, move it above  *
	 * this line and change the '@generated'-tag to '@generated NOT'.   *
	 * ---------------------------------------------------------------- */	

	/**
	 * The cached value of the '{@link #getClauses() <em>Clauses</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getClauses()
	 * @generated
	 * @ordered
	 */
	protected EList<Clause> clauses;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Alternation() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return IotaPackage.Literals.ALTERNATION;
	}

	/**
	 * Returns the value of the '<em><b>Clauses</b></em>' containment reference list.
	 * The list contents are of type {@link org.eclipse.iota.model.Clause}.
	 * It is bidirectional and its opposite is '{@link org.eclipse.iota.model.Clause#getAlternation <em>Alternation</em>}'.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @return the value of the '<em>Clauses</em>' containment reference list.
	 * @see org.eclipse.iota.IotaPackage#getAlternation_Clauses()
	 * @see org.eclipse.iota.model.Clause#getAlternation
	 * @model opposite="alternation" containment="true"
	 * @generated
	 */
	public EList<Clause> getClauses() {
		if (clauses == null) {
			clauses = new EObjectContainmentWithInverseEList<Clause>(Clause.class, this, IotaPackage.ALTERNATION__CLAUSES, IotaPackage.CLAUSE__ALTERNATION);
		}
		return clauses;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case IotaPackage.ALTERNATION__CLAUSES:
				return ((InternalEList<InternalEObject>)(InternalEList<?>)getClauses()).basicAdd(otherEnd, msgs);
		}
		return super.eInverseAdd(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case IotaPackage.ALTERNATION__CLAUSES:
				return ((InternalEList<?>)getClauses()).basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case IotaPackage.ALTERNATION__CLAUSES:
				return getClauses();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case IotaPackage.ALTERNATION__CLAUSES:
				getClauses().clear();
				getClauses().addAll((Collection<? extends Clause>)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case IotaPackage.ALTERNATION__CLAUSES:
				getClauses().clear();
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case IotaPackage.ALTERNATION__CLAUSES:
				return clauses != null && !clauses.isEmpty();
		}
		return super.eIsSet(featureID);
	}

} // Alternation
